package cn.jiyun.service.impl;

import cn.jiyun.entity.XmBusiness;
import cn.jiyun.mapper.XmBusinessMapper;
import cn.jiyun.service.IXmBusinessService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-25
 */
@Service
public class XmBusinessServiceImpl extends ServiceImpl<XmBusinessMapper, XmBusiness> implements IXmBusinessService {


    public Page<XmBusiness> findAll(Integer current, Integer pageSize, XmBusiness xmBusiness) {
        Page<XmBusiness> page = new Page<>(current,pageSize);
        QueryWrapper<XmBusiness> xmBusinessQueryWrapper = new QueryWrapper<>();

        if(xmBusiness.getId()!=null){
            xmBusinessQueryWrapper.eq("id",xmBusiness.getId());
        }
        if(xmBusiness.getMobilePhone()!=null && !"".equals(xmBusiness.getMobilePhone())){
            xmBusinessQueryWrapper.like("mobile_phone",xmBusiness.getMobilePhone());
        }
        if (xmBusiness.getZtime()!= null) {
            LocalDateTime now = LocalDateTime.now();
            int mo = 90;
            if (xmBusiness.getZtime() == 1) {
                xmBusinessQueryWrapper.apply("ABS(DATEDIFF({0}, registration)) <= {1}", now, mo);
            }

            if(xmBusiness.getZtime()==2){
                xmBusinessQueryWrapper.apply("YEAR(registration)=YEAR(CURRENT_DATE)");
            }

            if(xmBusiness.getZtime()==3){
                xmBusinessQueryWrapper.apply("DATE_FORMAT(registration,'%Y')=2022");
            }

            if(xmBusiness.getZtime()==4){
                xmBusinessQueryWrapper.apply("DATE_FORMAT(registration,'%Y')=2021");
            }
            if(xmBusiness.getZtime()==5){
                xmBusinessQueryWrapper.apply("DATE_FORMAT(registration,'%Y')=2020");
            }
            if(xmBusiness.getZtime()==6){
                xmBusinessQueryWrapper.apply("DATE_FORMAT(registration,'%Y')<2020");
            }
        }
        if(xmBusiness.getStatus()!=null){
            xmBusinessQueryWrapper.eq("status",xmBusiness.getStatus());
        }

        return baseMapper.selectPage(page,xmBusinessQueryWrapper);
    }

}
